package com.zhuozhengsoft.pageoffice6springboot2back.controller;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;

import com.zhuozhengsoft.pageoffice6springboot2back.util.GetDirPathUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping(value = "/DataRegionEdit")
public class DataRegionEditController {

    //获取doc目录的磁盘路径
    private String dir = GetDirPathUtil.getDirPath() + "static/doc/";

    @RequestMapping(value="/Word")
    public String openFile(HttpServletRequest request) {
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);

        WordDocument doc = new WordDocument();
        doc.getTemplate().defineDataRegion("PO_Guarantor", "[担保人]");
        doc.getTemplate().defineDataRegion("PO_SupplierAddress", "[供货单位地址]");
        doc.getTemplate().defineDataRegion("PO_BuyerAddress", "[购货单位地址]");
        doc.getTemplate().defineDataRegion("PO_No", "[合同编号]");
        doc.getTemplate().defineDataRegion("PO_GuarantorPhone", "[担保人手机]");
        doc.getTemplate().defineDataRegion("PO_ProductName", "[产品名称]");
        doc.getTemplate().defineDataRegion("PO_Buyer", "[购货单位]");
        doc.getTemplate().defineDataRegion("PO_Supplier", "[供货单位]");

        poCtrl.setWriter(doc); // 必须。

        poCtrl.setSaveFilePage("/DataRegionEdit/save");
        poCtrl.webOpen("/doc/DataRegionEdit/test.docx", OpenModeType.docNormalEdit, "张三");
        return  poCtrl.getHtmlCode();
    }

    @RequestMapping("save")
    public void save(HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        fs.saveToFile(dir + "DataRegionEdit/" + fs.getFileName());
        fs.close();
    }

}
